package com.qf.controller; /**
 * @author wgy
 * @version V1.0
 * @project java2402_xiaomi
 * @package ${PACKAGE_NAME}
 * @company 千锋教育
 * @date 2024/6/14 10:33
 */

import cn.hutool.core.util.StrUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet(urlPatterns="/goodsImgs")
public class GoodsImgsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String picture = request.getParameter("picture");
        if(StrUtil.isBlank(picture)){
            return;
        }
        String path = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images"+File.separator+picture);
        File file=new File(path);
        if(file.exists()){
            FileInputStream fis=new FileInputStream(file);
            ServletOutputStream os = response.getOutputStream();
            byte[] buf=new byte[1024*4];
            int len;
            while((len=fis.read(buf))!=-1){
                os.write(buf,0,len);
            }
            fis.close();
        }

    }
}
